package com.wangtao.firstapp.fragments.homefragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.jude.rollviewpager.RollPagerView;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.CustomMapAdapter;
import com.wangtao.firstapp.adapters.HomeVPAdapter;
import com.wangtao.firstapp.fragments.homepagerfragmnets.ArtFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.CateFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.DesignFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.GiftFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.OriginalSifeFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.SifeFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.StudentFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.WearFragment;
import com.wangtao.firstapp.fragments.homepagerfragmnets.WeekHotFragmet;
import com.wangtao.firstapp.modles.CustomMapData;
import com.wangtao.firstapp.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页主界面（轮播图在这里解析加载）
 * Created by lanouhn on 16/8/24.
 */
public class HomePagerFragment extends Fragment {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> hometabNames;
    private List<Fragment> homefragments;
    private HomeVPAdapter homeVPAdapter;

    private RollPagerView rollPagerView;
    private CustomMapData customMapData;
    private CustomMapAdapter customMapAdapter;

    private String url="http://open4.bantangapp.com/recommend/operationElement";
    private String params="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2560255&oauth_token=4df22230db195bba652ef0e593881122&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_pager, container, false);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout_home_pager_fragment);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager_home_pager_fragment);
        rollPagerView = (RollPagerView) view.findViewById(R.id.rollPagerView_home_pager_fragment);
        //设施播放时间间隔
        rollPagerView.setPlayDelay(1000 * 3);
        //设置透明度
        rollPagerView.setAnimationDurtion(500);
        initData();
        getDataFromWeb();
        return view;
    }

    private void initData() {

        hometabNames = new ArrayList<>();
        hometabNames.add("精选");
        hometabNames.add("原创精选");
        hometabNames.add("一周最热");
        hometabNames.add("美妆&穿搭");
        hometabNames.add("礼物");
        hometabNames.add("美食");
        hometabNames.add("设计感");
        hometabNames.add("文艺");
        hometabNames.add("学生党");

        homefragments = new ArrayList<>();
        SifeFragment sifeFragment = new SifeFragment();
        OriginalSifeFragment originalSifeFragment = new OriginalSifeFragment();
        WeekHotFragmet weekHotFragmet = new WeekHotFragmet();
        WearFragment wearFragment = new WearFragment();
        GiftFragment giftFragment = new GiftFragment();
        CateFragment cateFragment = new CateFragment();
        DesignFragment designFragment = new DesignFragment();
        ArtFragment artFragment = new ArtFragment();
        StudentFragment studentFragment = new StudentFragment();

        homefragments.add(sifeFragment);
        homefragments.add(originalSifeFragment);
        homefragments.add(weekHotFragmet);
        homefragments.add(wearFragment);
        homefragments.add(giftFragment);
        homefragments.add(cateFragment);
        homefragments.add(designFragment);
        homefragments.add(artFragment);
        homefragments.add(studentFragment);

        homeVPAdapter = new HomeVPAdapter(getFragmentManager(), hometabNames, homefragments);
        viewPager.setAdapter(homeVPAdapter);
        tabLayout.setupWithViewPager(viewPager);

    }

    private void getDataFromWeb() {
        try {
            HttpUtils.doPostAsyn(url,params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_cMap=new Gson();
                    customMapData=gson_cMap.fromJson(result,CustomMapData.class);
                    if (customMapData.getData().getBanner().size()>0){
                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    break;
                case 1:
                    customMapAdapter=new CustomMapAdapter(getActivity(),customMapData);
                    rollPagerView.setAdapter(customMapAdapter);
                    break;
            }
        }
    };


}
